package com.lemner.www.hongyu.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lemner.www.hongyu.Bean.Userbean;
import com.lemner.www.hongyu.Fragement.MyFragment;
import com.lemner.www.hongyu.Fragement.OrderFragment;
import com.lemner.www.hongyu.Fragement.SignFragment;
import com.lemner.www.hongyu.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_me,tv_order,tv_sign;
    private Button btn_dingdan,btn_qiangdan,btn_fadan;
    private ViewPager vp;
    private Userbean.DataBean date;
    private List<Fragment>fragment;
    private FragmentPagerAdapter fgadapter;
    private  MyFragment myFragment=new MyFragment();
    private  OrderFragment orderFragment=new OrderFragment();
    private  SignFragment signFragment=new SignFragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
        InitFragment();
        getDate();
        EjectPassword();
    }

    private void InitView() {
        tv_me=(TextView)findViewById(R.id.tv_me);
        tv_order=(TextView)findViewById(R.id.tv_order);
        tv_sign=(TextView)findViewById(R.id.tv_sign);
        btn_dingdan=(Button)findViewById(R.id.btn_dingdan);
        btn_qiangdan=(Button)findViewById(R.id.btn_qiangdan);
        btn_fadan=(Button)findViewById(R.id.btn_fadan);
        vp=(ViewPager)findViewById(R.id.view);
        tv_me.setOnClickListener(this);
        tv_order.setOnClickListener(this);
        tv_sign.setOnClickListener(this);
        btn_dingdan.setOnClickListener(this);
        btn_qiangdan.setOnClickListener(this);
        btn_fadan.setOnClickListener(this);
    }
    private void InitFragment() {
        fragment= new ArrayList<Fragment>();
        fragment.add(myFragment);
        fragment.add(orderFragment);
        fragment.add(signFragment);

        fgadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragment.get(position) ;
            }
            @Override
            public int getCount() {
                return fragment.size();
            }
        };
        vp.setAdapter(fgadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                resetTextcolor();
                changeTextcolor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void EjectPassword() {
        new AlertDialog.Builder(this)
                .setTitle("修改密码")
                .setMessage("需要修改系统默认密码，是否修改密码")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, ReviseActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

public void getDate(){
    date= (Userbean.DataBean)getIntent().getSerializableExtra("date");
    Bundle bundle =new Bundle();
    bundle.putSerializable("date",date);
    myFragment.setArguments(bundle);
    orderFragment.setArguments(bundle);
    signFragment.setArguments(bundle);
}
    @Override
    public void onClick(View v) {
        resetTextcolor();
        switch (v.getId()){
            case R.id.tv_me:
                changeTextcolor(0);
                break;
            case R.id.tv_order:
                changeTextcolor(1);
                break;
            case R.id.tv_sign:
                changeTextcolor(2);
                break;
            case R.id.btn_dingdan:
                Intent intent =new Intent(this,DingdanActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_qiangdan:
                break;
            case R.id.btn_fadan:
                Intent intent1 =new Intent(this,FadanActvivity.class);
                intent1.putExtra("date",date);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }
    public  void resetTextcolor(){
        tv_me.setTextColor(Color.rgb(255,255,255));
        tv_order.setTextColor(Color.rgb(255,255,255));
        tv_sign.setTextColor(Color.rgb(255,255,255));
    }
    public void changeTextcolor(int index){
        switch (index){
            case 0:
                tv_me.setTextColor(Color.rgb(0,0,0));
                break;
            case 1:
                tv_order.setTextColor(Color.rgb(0,0,0));
                break;
            case 2:
                tv_sign.setTextColor(Color.rgb(0,0,0));
                break;
            default:
                break;
        }
         vp.setCurrentItem(index);
    }


}
